
<template>
  <el-dialog v-model="ifShow" :title="modelTitle" width="1000" :before-close="handleCancel">
    <el-form ref="formRef" :model="formData" :rules="rules" label-width="auto" class="demo-ruleForm" status-icon>
      <el-form-item label="平台" prop="platformIds">
        <el-select multiple clearable v-model="formData.platformIds" placeholder="请选择">
          <el-option v-for="item in platFormArr" :key="item.value" :label="item.label" :value="item.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="渠道" prop="channelIds">
        <el-select multiple clearable v-model="formData.channelIds" placeholder="请选择">
          <el-option v-for="item in channelArr" :key="item.value" :label="item.label" :value="item.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="城市" prop="cityIds">
        <el-select multiple filterable clearable v-model="formData.cityIds" placeholder="请选择" @change="handleCityChange">
          <el-option v-for="item in cityOptions" :key="item.id" :label="item.name" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="业务类型" prop="orderTypes">
        <el-select multiple clearable v-model="formData.orderTypes" placeholder="请选择">
          <el-option v-for="item in orderTypeArr" :key="item.value" :label="item.label" :value="item.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="车型" prop="vehicleTypeIds">
        <el-select multiple filterable clearable v-model="formData.vehicleTypeIds" placeholder="请选择">
          <el-option v-for="item in carTypeArr" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="车系" prop="vehicleSeries">
        <el-select multiple filterable clearable v-model="formData.vehicleSeries" placeholder="请选择">
          <el-option v-for="item in carOptions" :key="item.id" :label="`${item.vehicleBrand}-${item.vehicleSeries}`" :value="item.id" />
        </el-select>
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="handleCancel">取消</el-button>
        <el-button type="primary" @click="handleOk">确认</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import CommonApi from "@/api/common";
import JntApi from "@/api/jnt";
import Supplier from "@/api/supplier";
import { ref } from "vue";
import { channelArr, bizTypeArr, orderTypeArr, platFormArr, carTypeArr } from "@/enums/common";
const carOptions = ref();
const props = defineProps<{}>();
const emit = defineEmits(["success"]);
const formRef: any = ref(null);
const formData: any = reactive({
  id: "",
  platformIds: undefined,
  channelIds: undefined,
  orderTypes: undefined,
  vehicleTypeIds: undefined,
  cityIds: undefined,
  vehicleSeries: undefined,
  remark: undefined,
});
const rules = reactive({
  platformIds: [{ required: true, message: "请选择!" }],
  channelIds: [{ required: true, message: "请选择!" }],
  orderTypes: [{ required: true, message: "请选择!" }],
  vehicleTypeIds: [{ required: true, message: "请选择!" }],
  cityIds: [{ required: true, message: "请选择!" }],
  vehicleSeries: [{ required: true, message: "请选择!" }],
  remark: [{ required: true, message: "请输入!" }],
});
const polyArr: any = ref([{ supplierId: "", percent: "1", mathNum: Date.now() + Math.random().toString(16).slice(2) }]); //里程价
const ifShow = ref(false);
const modelTitle = ref();
const cityOptions = ref();
function edit(title: any, row: any) {
  modelTitle.value = title;
  ifShow.value = true;

  if (!row.supplierConfig) {
    row.supplierConfig = {
      type: 1,
      percent: null,
    };
  }
  //赋值
  Object.assign(formData, row);
  formData.channelIds = [row.channelId];
  formData.bizTypes = [row.bizType];
  formData.platformIds = [row.platformId];
  formData.vehicleTypeIds = [row.vehicleTypeId];
  formData.cityIds = [row.cityId];
  formData.orderTypes = [row.orderType];
  //赋值
  let newCars: any = [];
  row.vehicleSeries.forEach((item: any) => {
    newCars.push(item.id);
  });
  formData.vehicleSeries = newCars;

  carOptions.value = carOptions.value.concat(row.vehicleSeries);
}

/**
 * 确定按钮点击事件
 */
async function handleOk() {
  await formRef.value.validate((valid: any, fields: any) => {
    if (valid) {
      let model: any = formData;
      JntApi.CheckCarEdit(model).then((res: any) => {
        if (res.code == 0) {
          ElMessage.success(res.message);
          formRef.value.resetFields();
          emit("success");
          handleCancel();
        }
      });
    } else {
      console.log("error submit!", fields);
    }
  });
}

//城市下拉框点击事件
function handleCityChange(value: any) {
  formData.cityName = cityOptions.value.find((item: any) => item.id == value).name;
}

/**
 * 取消按钮回调事件
 */
function handleCancel() {
  formRef.value.resetFields();
  ifShow.value = false;
}

defineExpose({
  edit,
});
onMounted(() => {
  CommonApi.getCity({ pageNo: 1, pageSize: 9999 }).then((data) => {
    if (data.code == 0) {
      cityOptions.value = data.result;
    }
  });
  CommonApi.getCars({ pageNo: 1, pageSize: 9999 }).then((data) => {
    if (data.code == 0) {
      carOptions.value = data.result;
    }
  });
});
</script>
<style lang="scss" scoped>
.flex_box {
  display: flex;
  margin-bottom: 14px;
  position: relative;
}
</style>
